package com.dameng.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dameng.system.entity.*;
import com.dameng.system.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/systemRole")
public class SystemRoleController {

    @Resource
    SystemRoleService systemRoleService;

    @Resource
    SystemMenuService systemMenuService;

    @Resource
    PermissionService permissionService;

    @Resource
    SystemRoleMenuService systemRoleMenuService;

    @Resource
    SystemUserRoleService systemUserRoleService;

    @GetMapping("/manager")
    public String manager() {
        return "/system/Role/manager";
    }

    @PostMapping("/list")
    @ResponseBody
    public Page<SystemRole> list(int page, int limit, SystemRole systemRole) {
        QueryWrapper<SystemRole> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(systemRole.getName())) {
            wrapper.lambda().like(SystemRole::getName, systemRole.getName());
        }
        Page<SystemRole> page1 = new Page<>(page, limit);
        return systemRoleService.page(page1, wrapper);
    }

    @PostMapping("/checkExist")
    @ResponseBody
    public boolean checkExist(String name) {
        int count = systemRoleService.count(
                new QueryWrapper<SystemRole>().lambda().eq(SystemRole::getName, name)
        );
        if (count > 0) {
            return true;
        }
        return false;
    }

    @GetMapping("/add")
    public String add(ModelMap model, SystemRole systemRole) {
        model.addAttribute("systemRole", systemRole);
        model.addAttribute("type", "save");
        return "/system/Role/edit";
    }

    @PostMapping("/save")
    @ResponseBody
    @Transactional
    public boolean save(SystemRole sysRole) {
        permissionService.cleanPermissionMap();
        return systemRoleService.save(sysRole);
    }

    @GetMapping("/edit")
    public String edit(ModelMap model, String roleId) {
        SystemRole systemRole = systemRoleService.getById(roleId);
        model.addAttribute("systemRole", systemRole);
        model.addAttribute("type", "update");
        return "/system/Role/edit";
    }

    @PostMapping("/update")
    @ResponseBody
    @Transactional
    public boolean update(SystemRole systemRole) {
        permissionService.cleanPermissionMap();
        return systemRoleService.updateById(systemRole);
    }

    @GetMapping("/editMenu")
    public String editMenu(ModelMap model, String roleId) {
        SystemRole systemRole = systemRoleService.getById(roleId);
        model.addAttribute("systemRole", systemRole);
        return "/system/Role/roleMenu";
    }

    @PostMapping("/menuList")
    @ResponseBody
    public List<SystemMenu> menuList(String roleId) {
        return systemMenuService.getMenusByRoleId(roleId);
    }

    @PostMapping("/saveMenus")
    @ResponseBody
    @Transactional
    public boolean saveMenus(String roleId, String... menuIds) {
        QueryWrapper<SystemRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SystemRoleMenu::getRoleId, roleId);
        systemRoleMenuService.remove(wrapper);

        List<SystemRoleMenu> systemRoleMenus = new ArrayList<>();
        List<String> ids = Arrays.asList(menuIds);
        for (String menuId : ids) {
            SystemRoleMenu roleMenu = new SystemRoleMenu().setMenuId(menuId).setRoleId(roleId);
            systemRoleMenus.add(roleMenu);
        }
        permissionService.cleanPermissionMap();
        return systemRoleMenuService.saveBatch(systemRoleMenus);
    }

    @PostMapping("/delete")
    @ResponseBody
    @Transactional
    public boolean delete(String... roleIds) {
        List<String> ids = Arrays.asList(roleIds);
        QueryWrapper<SystemRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.lambda().in(SystemRoleMenu::getRoleId, ids);
        systemRoleMenuService.remove(wrapper);

        QueryWrapper<SystemUserRole> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda().in(SystemUserRole::getRoleId, ids);
        systemUserRoleService.remove(wrapper1);

        permissionService.cleanPermissionMap();
        return systemRoleService.removeByIds(ids);
    }
}
